package gui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import simulation.Simulation;
import simulation.SimulationManager;
import user.User;
import utils.CurrentUser;
import utils.DateUtils;
import utils.Validator;

/**
 * Backing bean for starting a new simulation
 * 
 * CANT BE TESTED WITH JUNIT
 * 
 * @author Aleksander Zotov
 * 
 */
public class NewSimulationBean {

	private String name;
	private String description;
	private String collisionDistance;
	private String startTime;
	private String endTime;
	private String referenceData = "";

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setCollisionDistance(String collisionDistance) {
		this.collisionDistance = collisionDistance;
	}

	public String getCollisionDistance() {
		return collisionDistance;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setReferenceData(String referenceData) {
		this.referenceData = referenceData;
	}

	public String getReferenceData() {
		return referenceData;
	}

	/**
	 * Handles the uploading of reference objects as a .txt
	 * @param event
	 * @throws IOException
	 */
	public void handleFileUpload(FileUploadEvent event) throws IOException {
		UploadedFile file = event.getFile();
		String readLine;
		BufferedReader in = new BufferedReader(new InputStreamReader(file.getInputstream()));
		try {
			while((readLine = in.readLine())!=null) {
			referenceData += readLine;
			referenceData += System.getProperty("line.separator");
			}
			RequestContext context = RequestContext.getCurrentInstance();
			context.update("TLEPanel");
		} catch (IOException e) {
		}
		in.close();
	}

	/**
	 * Starts a new Simulation if all inputs are correct
	 */
	public void startNewSimulation() {
		MessageController messageController = new MessageController();
		if (CurrentUser.getCurrentUser().hasRunningSim()) {
			messageController.showErrorMessage("Eine bereits laufende Berechnung wurde noch nicht abgeschlossen!");
			return;
		}
		if (name.isEmpty() || description.isEmpty()
				|| collisionDistance.isEmpty() || collisionDistance.isEmpty()
				|| startTime.isEmpty() || endTime.isEmpty()
				|| referenceData.isEmpty()) {
			messageController
					.showErrorMessage("Eine oder mehrere Eingaben fehlen!");
			return;
		}
		if (!Validator.isValidSimName(name)) {
			messageController.showErrorMessage("Name ist zu  lang!");
			return;
		}
		if (!Validator.isValidSimDescription(description)) {
			messageController.showErrorMessage("Beschreibung ist zu lang!");
			return;
		}
		if (!Validator.isValidDistance(collisionDistance)) {
			messageController
					.showErrorMessage("Kollisionsabstand is ungültig!");
			return;
		}
		if (!Validator.isValidStartEnd(startTime, endTime)) {
			messageController
					.showErrorMessage("Berechnungszeitraum ist ungültig!");
			return;
		}
		if (!Validator.isValidTLE(referenceData.split("\\r?\\n"))) {
			messageController.showErrorMessage("Referenzdaten sind ungültig!");
			return;
		} else {
			//If al input data is valid...
			DateUtils dateUtils = new DateUtils();
			String refData[] = referenceData.split("\\n");
			Date startDate = dateUtils.toDate(startTime);
			Date endDate = dateUtils.toDate(endTime);
			float colDistance = Float.parseFloat(collisionDistance);
			User user = CurrentUser.getCurrentUser();
			Simulation newSim = new Simulation(name, description, colDistance,
					startDate, endDate, refData, user);
			try {
				//Start new simulation
				SimulationManager.getInstance().startNewSimulation(newSim);
			} catch (IOException e) {
				e.printStackTrace();
			}
			RequestContext context = RequestContext.getCurrentInstance();
			//Close dialog
			context.execute("newSimDialog.hide()");
		}
	}
	
	public void showNewSimulationDialog() {
		MessageController messageController = new MessageController();
		if(CurrentUser.getCurrentUser().hasRunningSim()) {
			messageController.showErrorMessage("Sie haben schon eine laufende Berechnung!");
		}
		else {
			RequestContext context = RequestContext.getCurrentInstance();
			context.execute("newSimDialog.show()");
		}
	}
}
